package com.huishi.ui;

import com.huishi.util.DBUtils;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * 数据迁移
 */
//@Component
public class DataMigrationUI extends JFrame {
    private JTextField databaseIPField;
    private JTextField portField;
    private JTextField dbNameField;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextArea filePathArea;

    public DataMigrationUI() {
        setTitle("数据迁移");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 初始化文本框
        databaseIPField = new JTextField(10);
        portField = new JTextField(10);
        dbNameField = new JTextField(10);
        usernameField = new JTextField(10);
        passwordField = new JPasswordField(10);
        filePathArea = new JTextArea(10, 30);
        filePathArea.setEditable(false);

        // 初始化按钮
        JButton testDBConnect = new JButton("测试连接");
        JButton selectFilesButton = new JButton("选择文件");
        JButton migrateButton = new JButton("开始迁移");

        // 添加按钮点击事件监听器
        selectFilesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 弹出文件选择框
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setMultiSelectionEnabled(true);
                int result = fileChooser.showOpenDialog(null);

                // 用户选择文件后，将文件路径显示在文本区域中
                if (result == JFileChooser.APPROVE_OPTION) {
                    File[] selectedFiles = fileChooser.getSelectedFiles();
                    StringBuilder sb = new StringBuilder();
                    for (File file : selectedFiles) {
                        sb.append(file.getAbsolutePath()).append("\n");
                    }
                    filePathArea.setText(sb.toString());
                }
            }
        });

        migrateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 开始迁移数据
                // 1

                // 获取用户输入的数据库连接信息和文件路径
                String databaseIP = databaseIPField.getText();
                String port = portField.getText();
                String dbName = dbNameField.getText();
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                String filePaths = filePathArea.getText();

                // 调用数据迁移方法，将文件数据迁移到数据库中
                // 迁移方法的实现需要根据具体情况进行
                // migrateData(databaseIP, port, dbName, username, password, filePaths);
                JOptionPane.showMessageDialog(null, "数据迁移已完成！", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        testDBConnect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 测试连接
                String testResult = DBUtils.testDBConnected("mysql",
                        databaseIPField.getText(),
                        portField.getText(),
                        usernameField.getText(),
                        passwordField.getText());

                // 弹出一个弹出框提示
                JOptionPane.showMessageDialog(null, testResult, "结果", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 设置布局为网格布局
        setLayout(new BorderLayout());

        // 创建面板用于放置文本框和按钮
        JPanel inputPanel = new JPanel(new GridLayout(3, 2,20,10));
        inputPanel.add(new JLabel("数据库IP：",SwingConstants.RIGHT));
        inputPanel.add(databaseIPField);
        inputPanel.add(new JLabel("端口号：",SwingConstants.RIGHT));
        inputPanel.add(portField);
        inputPanel.add(new JLabel("数据库名：",SwingConstants.RIGHT));
        inputPanel.add(dbNameField);
        inputPanel.add(new JLabel("用户名：",SwingConstants.RIGHT));
        inputPanel.add(usernameField);
        inputPanel.add(new JLabel("密码：",SwingConstants.RIGHT));
        inputPanel.add(passwordField);

        JLabel testDBText = new JLabel("连接测试：", SwingConstants.RIGHT);
        testDBText.setVisible(false);
        inputPanel.add(testDBText);

        inputPanel.add(testDBConnect);

        // 将文本区域放置在滚动面板中
        JScrollPane scrollPane = new JScrollPane(filePathArea);

        // 将文本区域和滚动面板放置在面板中
        JPanel filePanel = new JPanel(new BorderLayout());
        filePanel.add(selectFilesButton, BorderLayout.NORTH);
//        filePanel.add(new JLabel("已选文件："), BorderLayout.NORTH);
        filePanel.add(scrollPane, BorderLayout.CENTER);

        // 创建面板用于放置按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(migrateButton);

        //
        JPanel sub = new JPanel(new BorderLayout(0,15));
        sub.add(inputPanel,BorderLayout.NORTH);
        sub.add(filePanel,BorderLayout.SOUTH);

        // 将各个面板添加到窗口中
//        add(inputPanel, BorderLayout.NORTH);
//        add(filePanel, BorderLayout.CENTER);
        add(sub, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        // 创建并显示数据迁移界面
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DataMigrationUI().setVisible(true);
            }
        });
    }
}

